#include "loadUtils.h"
#include "adminprovLoad.h"
int adminprovLoad(const char *filename){
	FILE *ptr_file;
	ptr_file=fopen(filename,"r");
	if(!ptr_file){
		return 1;
	}
	int maxlinelen;
	int numlines=getFileInfo(ptr_file,&maxlinelen);
	adminprovcount=numlines;
	adminprovdata=(struct AdminProvVars*) calloc(numlines,sizeof(struct AdminProvVars));
	char *buf=(char*) malloc(maxlinelen*sizeof(char));
	for(int i=0;i<numlines;i++){
		fgets(buf,maxlinelen,ptr_file);//load line into buffer;
		adminprovdata[i].id=(int)*strtok(buf,"/");//get first token from buffer
		strcpy(adminprovdata[i].name,strtok(NULL,"/"));//subsequent tokens...
		strcpy(adminprovdata[i].email,strtok(NULL,"/"));
		strcpy(adminprovdata[i].password,strtok(NULL,"/"));
		strcpy(adminprovdata[i].type,strtok(NULL,"/"));
	}
	fclose(ptr_file);
	return 0;
}

int writeAdminProvs(char const *filename){
	FILE *ptr_file;
	ptr_file=fopen(filename,"w");
	if(!ptr_file){
		return 1;
	}
	for(int i=0;i<adminprovcount;i++){
		fprintf(ptr_file,"%d/%s/%s/%s/%s\n",adminprovdata[i].id,adminprovdata[i].name,adminprovdata[i].email,adminprovdata[i].password,adminprovdata[i].type);
	}
	fclose(ptr_file);
	free(adminprovdata);
	return 0;
}

void addAdminProv(struct AdminProvVars n){
	adminprovdata=(struct AdminProvVars*) realloc(adminprovdata,++adminprovcount*sizeof(struct AdminProvVars));
	adminprovdata[adminprovcount-1]=n;
}

void rmAdminProv(int index){
	for(int i=index;i<adminprovcount-1;i++){
		adminprovdata[i]=adminprovdata[i+1];
	}
	adminprovdata=(struct AdminProvVars*) realloc(adminprovdata,--adminprovcount*sizeof(struct AdminProvVars));
}
